Imports System.IO
Imports WinXound_Net.gVar

Public Class wxSettings

    Private mSymbol As String = "|"


    Public Sub LoadSettings()

        'Load Settings
        Try
            If File.Exists(Application.StartupPath & "\Settings\Settings.txt") Then

                Dim fi As New System.IO.FileInfo(Application.StartupPath & "\Settings\Settings.txt")
                If fi.Length <= 0 Then
                    CreateSettingsFile()
                    Exit Sub
                End If


                Dim reader As New StreamReader(Application.StartupPath & "\Settings\Settings.txt")
                Dim mString As String
                Dim delimStr As String = mSymbol
                Dim delimiter() As Char = delimStr.ToCharArray()

                Do Until reader.Peek = -1
                    mString = reader.ReadLine()

                    Dim split() As String = Nothing
                    split = mString.Split(delimiter)

                    Select Case split(0)
                        Case "FileCsound"
                            Preferences.Directory.CSoundConsole = split(1)
                        Case "FileWinsound"
                            Preferences.Directory.Winsound = split(1)
                        Case "FileCSoundAV"
                            Preferences.Directory.CSoundAV = split(1)
                        Case "FileHelpHTML"
                            Preferences.Directory.CSoundHelpHTML = split(1)
                        Case "FileHelpCSoundAV"
                            Preferences.Directory.CSoundAVHelp = split(1)
                        Case "WaveEditor"
                            Preferences.Directory.WaveEditor = split(1)
                        Case "WorkingDirectory"
                            Preferences.Directory.WorkingDir = split(1)
                        Case "SFDIR"
                            Preferences.Directory.SFDIR = split(1)
                        Case "SSDIR"
                            Preferences.Directory.SSDIR = split(1)
                        Case "SADIR"
                            Preferences.Directory.SADIR = split(1)
                        Case "MFDIR"
                            Preferences.Directory.MFDIR = split(1)
                        Case "INCDIR"
                            Preferences.Directory.INCDIR = split(1)
                        Case "DefaultFontName"
                            Preferences.Syntax.DefaultFontName = split(1)
                        Case "DefaultFontSize"
                            Preferences.Syntax.DefaultFontSize = split(1)
                        Case "TabSize"
                            Preferences.Syntax.DefaultTabSize = split(1)
                        Case "ColorText"
                            Preferences.Syntax.TextForeColor = split(1)
                        Case "ColorBackground"
                            Preferences.Syntax.TextBackColor = split(1)
                        Case "ColorSelectionFore"
                            Preferences.Syntax.TextSelectionForeColor = split(1)
                        Case "ColorSelectionBack"
                            Preferences.Syntax.TextSelectionBackColor = split(1)
                        Case "ColorOrchestra"
                            Preferences.Syntax.OpcodesForeColor = split(1)
                        Case "ColorOrchestraBK"
                            Preferences.Syntax.OpcodesBackColor = split(1)
                        Case "BoldOrchestra"
                            Preferences.Syntax.OpcodesBold = split(1)
                        Case "ItalicOrchestra"
                            Preferences.Syntax.OpcodesItalic = split(1)
                        Case "ColorCSD"
                            Preferences.Syntax.TagsForeColor = split(1)
                        Case "ColorCSDBK"
                            Preferences.Syntax.TagsBackColor = split(1)
                        Case "BoldCSD"
                            Preferences.Syntax.TagsBold = split(1)
                        Case "ItalicCSD"
                            Preferences.Syntax.TagsItalic = split(1)
                        Case "ColorMacro"
                            Preferences.Syntax.MacrosForeColor = split(1)
                        Case "ColorMacroBK"
                            Preferences.Syntax.MacrosBackColor = split(1)
                        Case "BoldMacro"
                            Preferences.Syntax.MacrosBold = split(1)
                        Case "ItalicMacro"
                            Preferences.Syntax.MacrosItalic = split(1)
                        Case "ColorInstrEndin"
                            Preferences.Syntax.InstrForeColor = split(1)
                        Case "ColorInstrEndinBK"
                            Preferences.Syntax.InstrBackColor = split(1)
                        Case "BoldInstr"
                            Preferences.Syntax.InstrBold = split(1)
                        Case "ItalicInstr"
                            Preferences.Syntax.InstrItalic = split(1)
                        Case "ColorFiles"
                            Preferences.Syntax.StringsForeColor = split(1)
                        Case "ColorFilesBK"
                            Preferences.Syntax.StringsBackColor = split(1)
                        Case "BoldStrings"
                            Preferences.Syntax.StringsBold = split(1)
                        Case "ItalicStrings"
                            Preferences.Syntax.StringsItalic = split(1)
                        Case "ColorReminders"
                            Preferences.Syntax.RemindersForeColor = split(1)
                        Case "ColorRemindersBK"
                            Preferences.Syntax.RemindersBackColor = split(1)
                        Case "BoldReminders"
                            Preferences.Syntax.RemindersBold = split(1)
                        Case "ItalicReminders"
                            Preferences.Syntax.RemindersItalic = split(1)
                        Case "ShowUtilitiesMessage"
                            Preferences.General.ShowUtilitiesMessage = split(1)
                        Case "ShowImportOrcScoMessage"
                            Preferences.General.ShowImportOrcScoMessage = split(1)
                        Case "WindowState"
                            If split(1) = "" Then split(1) = "2"
                            Preferences.General.WindowState = split(1)

                        Case "WindowSize"
                            Dim tPoint As Point
                            Dim dm() As Char = {"."}
                            Dim sp() As String = Nothing
                            sp = split(1).Split(dm)
                            tPoint.X = sp(0)
                            tPoint.Y = sp(1)
                            Preferences.General.WindowSize = tPoint

                        Case "WindowPosition"
                            Dim tPoint As Point
                            Dim dm() As Char = {"."}
                            Dim sp() As String = Nothing
                            sp = split(1).Split(dm)
                            tPoint.X = sp(0)
                            tPoint.Y = sp(1)
                            Preferences.General.WindowPosition = tPoint

                        Case "FirstStart"
                            Preferences.General.FirstStart = split(1)
                        Case "FindWholeWord"
                            Preferences.General.FindWholeWord = split(1)
                        Case "FindMatchCase"
                            Preferences.General.FindMatchCase = split(1)
                        Case "ReplaceFromCaret"
                            Preferences.General.ReplaceFromCaret = split(1)

                        Case "ShowTools"
                            Preferences.General.ShowTools = split(1)

                        Case "CSoundDefaultFlags"
                            Preferences.General.CSoundDefaultFlags = split(1)

                    End Select

                Loop

                reader.Close()

            Else
                CreateSettingsFile()
            End If

        Catch ex As Exception
            wxMessageError(ex.Message, "LoadSettings - Settings loader Error")
        End Try



        'Load 4 last files on LastFiles Array
        Try
            If File.Exists(Application.StartupPath & "\Settings\LastFiles.txt") Then
                Dim reader As New StreamReader(Application.StartupPath & "\Settings\LastFiles.txt")
                Dim mString As String
                Dim delimStr As String = mSymbol
                Dim delimiter() As Char = delimStr.ToCharArray()
                Dim mInd As Int32 = 0

                Do Until reader.Peek = -1
                    mString = reader.ReadLine()

                    Dim split() As String = Nothing
                    split = mString.Split(delimiter)

                    Select Case split(0)
                        Case "File0"
                            mInd = 0

                        Case "File1"
                            mInd = 1

                        Case "File2"
                            mInd = 2

                        Case "File3"
                            mInd = 3

                    End Select

                    If File.Exists(split(1)) Then
                        LastFiles(mInd) = split(1)
                    Else
                        LastFiles(mInd) = "..."
                    End If

                Loop

                Dim tempArray As New ArrayList
                For ciclo As Int32 = 0 To 3
                    If LastFiles(ciclo) <> "..." Then tempArray.Add(LastFiles(ciclo))
                Next

                'Array.Clear(LastFiles, 0, LastFiles.Length)
                For ciclo As Int32 = 0 To 3
                    If ciclo < tempArray.Count Then
                        LastFiles(ciclo) = tempArray.Item(ciclo)
                    Else
                        LastFiles(ciclo) = "..."
                    End If
                Next

                reader.Close()
            Else
                CreateLastFilesFile()
            End If

        Catch ex As Exception
            wxMessageError(ex.Message, "LoadSettings - LastFiles loader Error")
        End Try
        

        Try
            System.Environment.SetEnvironmentVariable("SFDIR", Preferences.Directory.SFDIR)
            System.Environment.SetEnvironmentVariable("SSDIR", Preferences.Directory.SSDIR)
            System.Environment.SetEnvironmentVariable("SADIR", Preferences.Directory.SADIR)
            System.Environment.SetEnvironmentVariable("MFDIR", Preferences.Directory.MFDIR)
            System.Environment.SetEnvironmentVariable("INCDIR", Preferences.Directory.INCDIR)
        Catch ex As Exception
            wxMessageError(ex.Message, "LoadSettings - SetEnvironmentVariable Error")
        End Try

    End Sub




    Public Sub SaveSettings()

        Try

            Dim writer As New StreamWriter(Application.StartupPath & "\Settings\Settings.txt", False)

            'Directories
            writer.WriteLine("FileCsound" & mSymbol & Preferences.Directory.CSoundConsole)
            writer.WriteLine("FileWinsound" & mSymbol & Preferences.Directory.Winsound)
            writer.WriteLine("FileCSoundAV" & mSymbol & Preferences.Directory.CSoundAV)
            writer.WriteLine("FileHelpHTML" & mSymbol & Preferences.Directory.CSoundHelpHTML)
            writer.WriteLine("FileHelpCSoundAV" & mSymbol & Preferences.Directory.CSoundAVHelp)
            writer.WriteLine("WaveEditor" & mSymbol & Preferences.Directory.WaveEditor)
            writer.WriteLine("WorkingDirectory" & mSymbol & Preferences.Directory.WorkingDir)

            writer.WriteLine("SFDIR" & mSymbol & Preferences.Directory.SFDIR)
            writer.WriteLine("SSDIR" & mSymbol & Preferences.Directory.SSDIR)
            writer.WriteLine("SADIR" & mSymbol & Preferences.Directory.SADIR)
            writer.WriteLine("MFDIR" & mSymbol & Preferences.Directory.MFDIR)
            writer.WriteLine("INCDIR" & mSymbol & Preferences.Directory.INCDIR)

            ''Syntax and Text
            writer.WriteLine("DefaultFontName" & mSymbol & Preferences.Syntax.DefaultFontName)
            writer.WriteLine("DefaultFontSize" & mSymbol & Preferences.Syntax.DefaultFontSize)
            writer.WriteLine("TabSize" & mSymbol & Preferences.Syntax.DefaultTabSize)

            writer.WriteLine("ColorText" & mSymbol & Preferences.Syntax.TextForeColor)
            writer.WriteLine("ColorBackground" & mSymbol & Preferences.Syntax.TextBackColor)
            writer.WriteLine("ColorSelectionFore" & mSymbol & Preferences.Syntax.TextSelectionForeColor)
            writer.WriteLine("ColorSelectionBack" & mSymbol & Preferences.Syntax.TextSelectionBackColor)

            writer.WriteLine("ColorOrchestra" & mSymbol & Preferences.Syntax.OpcodesForeColor)
            writer.WriteLine("ColorOrchestraBK" & mSymbol & Preferences.Syntax.OpcodesBackColor)
            writer.WriteLine("BoldOrchestra" & mSymbol & Preferences.Syntax.OpcodesBold)
            writer.WriteLine("ItalicOrchestra" & mSymbol & Preferences.Syntax.OpcodesItalic)

            writer.WriteLine("ColorCSD" & mSymbol & Preferences.Syntax.TagsForeColor)
            writer.WriteLine("ColorCSDBK" & mSymbol & Preferences.Syntax.TagsBackColor)
            writer.WriteLine("BoldCSD" & mSymbol & Preferences.Syntax.TagsBold)
            writer.WriteLine("ItalicCSD" & mSymbol & Preferences.Syntax.TagsItalic)

            writer.WriteLine("ColorMacro" & mSymbol & Preferences.Syntax.MacrosForeColor)
            writer.WriteLine("ColorMacroBK" & mSymbol & Preferences.Syntax.MacrosBackColor)
            writer.WriteLine("BoldMacro" & mSymbol & Preferences.Syntax.MacrosBold)
            writer.WriteLine("ItalicMacro" & mSymbol & Preferences.Syntax.MacrosItalic)

            writer.WriteLine("ColorInstrEndin" & mSymbol & Preferences.Syntax.InstrForeColor)
            writer.WriteLine("ColorInstrEndinBK" & mSymbol & Preferences.Syntax.InstrBackColor)
            writer.WriteLine("BoldInstr" & mSymbol & Preferences.Syntax.InstrBold)
            writer.WriteLine("ItalicInstr" & mSymbol & Preferences.Syntax.InstrItalic)

            writer.WriteLine("ColorFiles" & mSymbol & Preferences.Syntax.StringsForeColor)
            writer.WriteLine("ColorFilesBK" & mSymbol & Preferences.Syntax.StringsBackColor)
            writer.WriteLine("BoldStrings" & mSymbol & Preferences.Syntax.StringsBold)
            writer.WriteLine("ItalicStrings" & mSymbol & Preferences.Syntax.StringsItalic)

            writer.WriteLine("ColorReminders" & mSymbol & Preferences.Syntax.RemindersForeColor)
            writer.WriteLine("ColorRemindersBK" & mSymbol & Preferences.Syntax.RemindersBackColor)
            writer.WriteLine("BoldReminders" & mSymbol & Preferences.Syntax.RemindersBold)
            writer.WriteLine("ItalicReminders" & mSymbol & Preferences.Syntax.RemindersItalic)

            'General
            writer.WriteLine("ShowUtilitiesMessage" & mSymbol & Preferences.General.ShowUtilitiesMessage)
            writer.WriteLine("ShowImportOrcScoMessage" & mSymbol & Preferences.General.ShowImportOrcScoMessage)
            If FormMain.WindowState = FormWindowState.Minimized Then FormMain.WindowState = FormWindowState.Normal
            writer.WriteLine("WindowState" & mSymbol & FormMain.WindowState)
            writer.WriteLine("WindowSize" & mSymbol & FormMain.Width & "." & FormMain.Height)
            writer.WriteLine("WindowPosition" & mSymbol & FormMain.Location.X & "." & FormMain.Location.Y)
            writer.WriteLine("FirstStart" & mSymbol & Preferences.General.FirstStart)
            writer.WriteLine("FindWholeWord" & mSymbol & Preferences.General.FindWholeWord)
            writer.WriteLine("FindMatchCase" & mSymbol & Preferences.General.FindMatchCase)
            writer.WriteLine("ReplaceFromCaret" & mSymbol & Preferences.General.ReplaceFromCaret)
            writer.WriteLine("CSoundDefaultFlags" & mSymbol & Preferences.General.CSoundDefaultFlags)

            'Save Window View preferences (LineNumbers, Eplorer, OnlineHelp, ToolBar)
            Dim tempString As String = ""
            tempString = BoolToByte(FormMain.SciEdit1.LineNumbers)
            tempString &= BoolToByte(FormMain.ToolStrip1.Visible)
            tempString &= BoolToByte(FormMain.wxPanel1.Visible)
            tempString &= BoolToByte(FormMain.wxIntelliTip1.Visible)
            writer.WriteLine("ShowTools" & mSymbol & tempString)

            writer.Close()

        Catch ex As Exception
            wxMessageError(ex.Message, "SaveSettings - Settings writer Error")
        End Try


        Try
            System.Environment.SetEnvironmentVariable("SFDIR", Preferences.Directory.SFDIR)
            System.Environment.SetEnvironmentVariable("SSDIR", Preferences.Directory.SSDIR)
            System.Environment.SetEnvironmentVariable("SADIR", Preferences.Directory.SADIR)
            System.Environment.SetEnvironmentVariable("MFDIR", Preferences.Directory.MFDIR)
            System.Environment.SetEnvironmentVariable("INCDIR", Preferences.Directory.INCDIR)
        Catch ex As Exception
            wxMessageError(ex.Message, "SaveSettings - SetEnvironmentVariable Error")
        End Try


    End Sub


    Public Sub SaveRecentFiles()

        Try
            Dim writer As New StreamWriter(Application.StartupPath & "\Settings\LastFiles.txt", False)

            'Last Files loaded
            writer.WriteLine("File0" & mSymbol & LastFiles(0))
            writer.WriteLine("File1" & mSymbol & LastFiles(1))
            writer.WriteLine("File2" & mSymbol & LastFiles(2))
            writer.WriteLine("File3" & mSymbol & LastFiles(3))
            writer.Close()

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Error")
        End Try

    End Sub



    Private Sub CreateSettingsFile()

        Preferences.Syntax.DefaultFontName = "Courier New"
        Preferences.Syntax.DefaultFontSize = 10
        Preferences.Syntax.DefaultTabSize = 8
        Preferences.Syntax.TextForeColor = RGB(0, 0, 0)
        Preferences.Syntax.TextBackColor = RGB(255, 255, 255)
        Preferences.Syntax.TextSelectionForeColor = RGB(0, 0, 0)
        Preferences.Syntax.TextSelectionBackColor = RGB(192, 192, 192)

        Preferences.Syntax.OpcodesForeColor = RGB(0, 0, 255)
        Preferences.Syntax.OpcodesBackColor = RGB(255, 255, 255)
        Preferences.Syntax.OpcodesBold = False
        Preferences.Syntax.OpcodesItalic = False

        Preferences.Syntax.TagsForeColor = RGB(220, 0, 110)
        Preferences.Syntax.TagsBackColor = RGB(255, 255, 255)
        Preferences.Syntax.TagsBold = False
        Preferences.Syntax.TagsItalic = False

        Preferences.Syntax.MacrosForeColor = RGB(160, 50, 160)
        Preferences.Syntax.MacrosBackColor = RGB(255, 255, 255)
        Preferences.Syntax.MacrosBold = False
        Preferences.Syntax.MacrosItalic = False

        Preferences.Syntax.InstrForeColor = RGB(0, 0, 255)
        Preferences.Syntax.InstrBackColor = RGB(255, 255, 255)
        Preferences.Syntax.InstrBold = False
        Preferences.Syntax.InstrItalic = False

        Preferences.Syntax.StringsForeColor = RGB(150, 0, 0)
        Preferences.Syntax.StringsBackColor = RGB(255, 255, 255)
        Preferences.Syntax.StringsBold = False
        Preferences.Syntax.StringsItalic = False

        Preferences.Syntax.RemindersForeColor = RGB(0, 150, 0)
        Preferences.Syntax.RemindersBackColor = RGB(255, 255, 255)
        Preferences.Syntax.RemindersBold = False
        Preferences.Syntax.RemindersItalic = False

        Preferences.General.FirstStart = True
        Preferences.General.WindowState = 2
        Preferences.General.WindowSize = New Point(600, 400)
        Preferences.General.WindowPosition = New Point(0, 0)
        Preferences.General.ShowTools = "1111"
        Preferences.General.CSoundDefaultFlags = "-B4096 --displays --asciidisplay"

        Me.SaveSettings()

    End Sub

    Private Sub CreateLastFilesFile()

        For ciclo As Int32 = 0 To 3
            LastFiles(ciclo) = "..."
        Next

        Me.SaveRecentFiles()

    End Sub

    Private Function BoolToByte(ByVal mBool As Boolean) As Byte
        If mBool = True Then
            Return 1
        Else
            Return 0
        End If

    End Function


End Class
